package com.itWaiLong.Controller.User;

import com.itWaiLong.Dao.Entities.UserLogin;
import com.itWaiLong.Dao.User;
import com.itWaiLong.Result.Result;
import com.itWaiLong.ReturnString.ResponseData;
import com.itWaiLong.Service.UserService;
import com.itWaiLong.Utils.JWTUtils;
import com.itWaiLong.Utils.Md5Utils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * Controller介绍
 *      该Controller用于用户个人和管理人员的注册/登录、编辑个人信息、自行退出（删除用户信息）、
 *      登录完成后完善个人信息等的功能开发
 */

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 用户注册
     * @param user 注册信息
     * @return 返回注册状态
     */
    @PostMapping("/register")
    public Result register(@RequestBody User user) {
        log.info("用户注册时的信息为{}", user);

        User getUser = userService.findUserByUserName(user.getUsername());
        if (getUser != null) {
            return Result.fail(ResponseData.YI_CUN_ZAI_YONG_HU);//用户已经存在
        }

        int status = userService.addUser(user);
        if (status > 0){
            return Result.success(ResponseData.ZHU_CE_CHENG_GONG);//注册成功
        }
        return Result.fail(ResponseData.ZHU_CE_SHI_BAI);//注册失败
    }

    /**
     * 用户登录
     * @param userLogin 登录时的数据
     * @return 返回登录状态
     */
    @PostMapping("/login")
    public Result login(@RequestBody UserLogin userLogin) {

        log.info("用户登录时的信息：{}", userLogin);

        String username = userLogin.getUsername();
        String password = userLogin.getPassword();

        //查找用户是否存在
        User userByUserName = userService.findUserByUserName(username);
        if (userByUserName == null){
            return Result.fail(ResponseData.YONG_HU_BU_CUN_ZAI);
        }

        //对提交的密码进行加密
        String md5Password = Md5Utils.getMd5(password);

        //判断密码
        if(!userByUserName.getPassword().equals(md5Password)){
            return Result.fail(ResponseData.MI_MA_CUO_WU);
        }

        //登录成功
        Map<String, Object> map = new HashMap<>();
        map.put("userId",userByUserName.getUserId());
        map.put("userType",userByUserName.getUserType());

        String token = JWTUtils.SetToken(map);
        return Result.success(token);
    }

    /**
     * 编辑用户信息
     * @param user 编辑的信息
     * @return 返回编辑状态
     */
    @PostMapping("/updateUserInfo")
    public Result updateUserInfo(@RequestBody User user) {
        log.info("编辑用户时的信息{}", user);

        int result = userService.updateUserInfo(user);
        if (result > 0){
            return Result.success();
        }
        return Result.fail(ResponseData.CAO_ZUO_SHI_BAI);
    }


    @GetMapping("/deleteUserInfo")
    public Result DeleteUserInfo(){
        /*//判断用户是否存在
        User userByUserName = userService.findUserByUserName(username);
        if (userByUserName != null){
            return Result.fail(ResponseData.YONG_HU_BU_CUN_ZAI);
        }*/

        //用户存在，删除用户
        int result = userService.deleteUserInfo();
        if (result > 0){
            return Result.success();
        }
        if (result == -1){
            return Result.fail(ResponseData.YONG_HU_BU_CUN_ZAI);
        }
        return Result.fail(ResponseData.CAO_ZUO_SHI_BAI);
    }
}
